﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace HaGisPack
{
    [Serializable]
    public class FBitmapLayer : FImageLayer        
    {

        public FBitmapLayer(string id)
            : base(id)
        { 
        }

        public FBitmapLayer(string id, string filename, System.Drawing.PointF leftbottomPt, System.Drawing.PointF rightupPt,
            System.Drawing.PointF p1, System.Drawing.PointF p2)
            : base(id)
        {
           

            //this.DataSet = new DotSpatial.Data.ImageData();
            //(this.DataSet as DotSpatial.Data.ImageData).Open(filename);
          
            this._leftbottomPt = leftbottomPt;
            this._rightupPt = rightupPt;
            this.p1 = p1;
            this.p2 = p2;

            this.sourceFilename = filename;

            this.m_bShapeEditable = false;

            //(this.DataSet as DotSpatial.Data.ImageData).WorldFile.TopLeftX = p1.X;
            //(this.DataSet as DotSpatial.Data.ImageData).WorldFile.TopLeftY = p2.Y;
            //(this.DataSet as DotSpatial.Data.ImageData).WorldFile.CellWidth = (p2.X - p1.X) / (rightupPt.X - leftbottomPt.X);
            //(this.DataSet as DotSpatial.Data.ImageData).WorldFile.CellHeight = -(p2.Y - p1.Y) / (rightupPt.Y - leftbottomPt.Y);

            //(this.DataSet as DotSpatial.Data.ImageData).BandType = DotSpatial.Data.ImageBandType.ARGB;

        }

        //位图的左下角和右上角
        PointF _leftbottomPt = new PointF();
        PointF _rightupPt = new PointF();

        //位图的左下角和右上角的绝对坐标
        PointF p1 = new PointF();
        PointF p2 = new PointF();

        /// <summary>
        /// 源路径
        /// </summary>
        string sourceFilename = "";

        /// <summary>
        /// 项目路径
        /// </summary>
        string destFilename = "";

        string worldFilename = "";

        protected override void BuildLayer()
        {

            base.BuildLayer();
            this.DataSet = new DotSpatial.Data.ImageData();
            if (this.destFilename == "")
            {
                this.destFilename = this.m_ParenLManager.layerPath + System.IO.Path.GetFileName(this.sourceFilename);

                System.IO.File.SetAttributes(this.sourceFilename, System.IO.FileAttributes.Normal);

                System.IO.File.Copy(this.sourceFilename, this.destFilename);
            }


            (this.DataSet as DotSpatial.Data.ImageData).Open(destFilename);
           
            

            //(this.DataSet as DotSpatial.Data.ImageData).Filename = this.destFilename;


            (this.DataSet as DotSpatial.Data.ImageData).WorldFile.TopLeftX = this.p1.X;
            (this.DataSet as DotSpatial.Data.ImageData).WorldFile.TopLeftY = this.p2.Y;
            (this.DataSet as DotSpatial.Data.ImageData).WorldFile.CellWidth = (this.p2.X - this.p1.X) / (this._rightupPt.X - this._leftbottomPt.X);
            (this.DataSet as DotSpatial.Data.ImageData).WorldFile.CellHeight = -(p2.Y - p1.Y) / (this._rightupPt.Y - this._leftbottomPt.Y);

            (this.DataSet as DotSpatial.Data.ImageData).BandType = DotSpatial.Data.ImageBandType.ARGB;


            this.ILayer = this.m_ParenLManager.CurrentMap.Layers.Add(this.DataSet);

            
             //this .worldFilename =this .m_ParenLManager .layerPath +System .IO .Path .GetFileNameWithoutExtension (this .sourceFilename )+"."

            (this.DataSet as DotSpatial.Data.ImageData).Filename = this.destFilename;

        }


        public override void SaveEntity()           
        {
            try
            {
                (this.DataSet as DotSpatial.Data.ImageData).Save();
                (this.DataSet as DotSpatial.Data.ImageData).WorldFile.Save();
            }
            catch (Exception ex)
            {
            }

        }

        public override void LoadEntity()
        {
            base.LoadEntity();

            //this.DataSet = new DotSpatial.Data.ImageData();
            //(this.DataSet as DotSpatial.Data.ImageData).Open(this.destFilename);
            //(this.DataSet as DotSpatial.Data.ImageData).WorldFile.Open();
        }
       

    }
}
